/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.uac.pages.entitytype;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.eelpo.cmmp.core.exception.ServiceException;
import com.eelpo.cmmp.core.wicket.behavior.JavascriptEventConfirmation;
import com.eelpo.cmmp.core.wicket.page.extensions.SerachListWebPage;
import com.eelpo.cmmp.core.wicket.panel.grid.column.ColumnLocation;
import com.eelpo.cmmp.core.wicket.panel.grid.column.ColumnLocation.Unit;
import com.eelpo.cmmp.core.wicket.panel.grid.column.IColumnHeader;
import com.eelpo.cmmp.core.wicket.panel.grid.column.OperateColumn;
import com.eelpo.cmmp.core.wicket.panel.grid.column.PropertyColumn;
import com.eelpo.cmmp.uac.dao.vo.EntityType;
import com.eelpo.cmmp.uac.dao.vo.enumed.Status;
import com.eelpo.cmmp.uac.services.IEntityTypeManageService;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.Loop.LoopItem;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 * <pre>
 * 实体类型管理页面
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
public class EntityTypeManage extends SerachListWebPage<EntityType> {

	@SpringBean
	private IEntityTypeManageService entityTypeManageService;

	public EntityTypeManage() {
		super("实体类型管理", 15, new EntityType());
	}

	@Override
	public Panel renderSearchPanel(String id) {
		return new EntityTypeSearchPanel<EntityType>(id, getParameter());
	}

	@Override
	public Iterator<? extends EntityType> iterator(EntityType parameter, int first, int count) throws Exception {
		return entityTypeManageService.getEntityTypesPaing(parameter, first, count).iterator();
	}

	@Override
	public int totalRows(EntityType parameter) throws Exception {
		return entityTypeManageService.getEntityTypesCount(parameter);
	}

	@Override
	public List<IColumnHeader<EntityType>> renderColumns() {
		List<IColumnHeader<EntityType>> columns = new ArrayList<IColumnHeader<EntityType>>();
		columns.add(new PropertyColumn<EntityType>("类型名称", "name", true, new ColumnLocation(25, Unit.PERCENT)));
		columns.add(new PropertyColumn<EntityType>("类型代码", "code", true, new ColumnLocation(25, Unit.PERCENT)));
		columns.add(new PropertyColumn<EntityType>("类型状态", "status.label", "status", true, new ColumnLocation(25, Unit.PERCENT)));
		columns.add(new OperateColumn<EntityType>(3, new ColumnLocation(25, Unit.PERCENT)) {

			private static final long serialVersionUID = 3148885718228823380L;

			@Override
			public void populateItem(String id, String labelId, LoopItem item, IModel<EntityType> model) {
				switch (item.getIteration()) {
				case 0:
					item.add(renderStatusLink(id, labelId, model));
					break;
				case 1:
					item.add(renderEditLink(id, labelId, model));
					break;
				case 2:
					item.add(renderDeleteLink(id, labelId, model));
					break;
				}
			}
		});
		return columns;
	}

	public MarkupContainer renderStatusLink(String id, String labelId, final IModel<EntityType> model) {

		final IModel<String> labelModel = new Model<String>(Status.ENABLED.equals(model.getObject().getStatus()) ? Status.DISABLED.getLabel() : Status.ENABLED.getLabel());

		Link<EntityType> statusLink = new Link<EntityType>(id) {

			private static final long serialVersionUID = 2150068897967082946L;

			@Override
			public void onClick() {
				try {
					EntityType entityType = model.getObject();
					switch (entityType.getStatus()) {
					case ENABLED:
						entityType.setStatus(Status.DISABLED);
						entityTypeManageService.disabledEntityType(entityType);
						labelModel.setObject(Status.ENABLED.getLabel());
						break;
					case DISABLED:
						entityType.setStatus(Status.ENABLED);
						entityTypeManageService.enabledEntityType(entityType);
						labelModel.setObject(Status.DISABLED.getLabel());
						break;
					}
				} catch (ServiceException ex) {
					info(ex.getMessage());
				} catch (Exception e) {
					e.printStackTrace();
					info("修改实体类型状态失败！");
				}
			}
		};

		// statusLink.setEnabled(CMMPSession.get().checkPermission("", ""));

		statusLink.add(new JavascriptEventConfirmation("您确定要修改该实体类型的状态吗？", statusLink.isEnabled()));

		return statusLink.add(new Label(labelId, labelModel));
	}

	public MarkupContainer renderEditLink(String id, String labelId, final IModel<EntityType> model) {
		return new Link<EntityType>(id) {

			private static final long serialVersionUID = -3557037551388507567L;

			@Override
			public void onClick() {
				setResponsePage(new AddOrEditEntityType(model.getObject()));
			}
		}.add(new Label(labelId, "编辑"));
	}

	public MarkupContainer renderDeleteLink(String id, String labelId, final IModel<EntityType> model) {
		Link<EntityType> deleteLink = new Link<EntityType>(id) {

			private static final long serialVersionUID = 7952411300896859397L;

			@Override
			public void onClick() {
				try {
					entityTypeManageService.deleteEntityType(model.getObject());
				} catch (ServiceException ex) {
					info(ex.getMessage());
				} catch (Exception e) {
					e.printStackTrace();
					info("删除实体类型失败！");
				}
			}
		};

		// deleteLink.setEnabled(CMMPSession.get().checkPermission("", ""));

		deleteLink.add(new JavascriptEventConfirmation("您确定要删除该实体类型吗？", deleteLink.isEnabled()));

		return deleteLink.add(new Label(labelId, "删除"));
	}
}
